<?php

namespace App\Admin\Controllers;

use DB;
use Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\AdminPermission;

class PermissionController extends Controller
{
	/**
	 * [index description]
	 * @return [type] [description]
	 */
	public function index(Request $request)
	{
		$permissions = \App\AdminPermission::where('cid', '=', 0);
		if ($request->input('keywords')) {
			$keywords = $request->input('keywords');
			$permissions = $permissions->where(function($permissions) use ($keywords) {
				$permissions->where('label', 'like', '%' . $keywords . '%')->orWhere('name', 'like', '%' . $keywords . '%')->orWhere('description', 'like', '%' . $keywords . '%');
			});
		}
		$permissions = $permissions->paginate(10);
		$permissions = $permissions->appends([
				'keywords' => $request->input('keywords')
			]);

		return view('/admin/permission/index', compact('permissions'));
	}

	/**
	 * [create description]
	 * @return [type] [description]
	 */
	public function create()
	{
		$permissions = AdminPermission::select(DB::raw('id,label'))->where('cid', '=', 0)->get()->toArray();
		$permissions = array_pluck($permissions, 'label', 'id');

		return view('/admin/permission/add', compact('permissions'));
	}

	/**
	 * [store description]
	 * @return [type] [description]
	 */
	public function store(Request $request)
	{
        $validator = Validator::make($request->all(), [
			'name' => 'required',
			'label' => 'required',
			'description' => 'required',
			'icon' => 'required'
        ], [
        	'name.required' => '请填写权限规则',
        	'label.required' => '请填写权限名称',
        	'description.required' => '请填写权限描述',
        	'icon' => '请选择图标'
        	]);

        if ($validator->passes()) {
			$name = $request->input('name');
			$description = $request->input('description');
			$label = $request->input('label');
			$cid = $request->input('cid') ? $request->input('cid'): 0;
			$icon = $request->input('icon') ? $request->input('icon'): 'fa-circle-o';
			\App\AdminPermission::create(request(['name', 'description', 'label', 'cid', 'icon']));

			if ($cid == 0) {
				return redirect('/admin/permissions');
			} else {
				return redirect('/admin/permissions/sub_permission/'.$cid);
			}
        } else {
        	return Redirect::back()->withInput()->withErrors($validator->errors());
        }
	}

	/**
	 * [edit description]
	 * @param  AdminPermission $permission [description]
	 * @return [type]                      [description]
	 */
	public function edit(AdminPermission $permission)
	{
		$permissions = AdminPermission::select(DB::raw('id,label'))->where('cid', '=', 0)->get()->toArray();
		$permissions = array_pluck($permissions, 'label', 'id');

		return view('/admin/permission/edit', compact('permission', 'permissions'));
	}

	/**
	 * [editStore description]
	 * @param  AdminPermission $permission [description]
	 * @return [type]                      [description]
	 */
	public function editStore(Request $request, AdminPermission $permission)
	{
        $validator = Validator::make($request->all(), [
			'name' => 'required',
			'label' => 'required',
			'description' => 'required'
        ], [
        	'name.required' => '请填写权限规则',
        	'label.required' => '请填写权限名称',
        	'description.required' => '请填写权限描述'
        	]);

        if ($validator->passes()) {
        	$permission->name = $request->input('name');
        	$permission->label = $request->input('label');
        	$permission->description = $request->input('description');
        	if ($request->input('cid')) {
        		$permission->cid = $request->input('cid');
        	}
        	if ($request->input('icon')) {
        		$permission->icon = $request->input('icon');
        	}
        	$permission->save();

			return redirect('/admin/permissions');
        } else {
        	return Redirect::back()->withInput()->withErrors($validator->errors());
        }	
	}

	/**
	 * [subPermission description]
	 * @return [type] [description]
	 */
	public function subPermission(Request $request, AdminPermission $permission)
	{
		$permissions = AdminPermission::where('cid', '=', $permission->id);
		if ($request->input('keywords')) {
			$keywords = $request->input('keywords');
			$permissions = $permissions->where(function($permissions) use ($keywords) {
				$permissions->where('label', 'like', '%' . $keywords . '%')->orWhere('name', 'like', '%' . $keywords . '%')->orWhere('description', 'like', '%' . $keywords . '%');
			});
		}
		$permissions = $permissions->paginate(10);
		$permissions = $permissions->appends([
				'keywords' => $request->input('keywords')
			]);
		return view('/admin/permission/subPer', compact('permissions', 'permission'));
	}

	/**
	 * [delete description]
	 * @param  AdminPermission $permission [description]
	 * @return [type]                      [description]
	 */
	public function delete(AdminPermission $permission)
	{
		$flag = false;
		$msg = '';
		if ($permission->subClass()->isEmpty()) {
			if ($permission->roles->isEmpty()) {
				$permission->delete();
				$flag = true;
			} else {
				$msg = '此权限已分配给角色，无法删除!';				
			}
		} else {
			$msg = '此权限下还有下级权限，无法删除!';
		}
		return [
			'flag' => $flag,
			'msg' => $msg,
			'pid' => $permission->id
		];
	}
}